package org.cytoscape.myapp.cytoscape_app_v1.internal;

import java.util.Properties;

import org.cytoscape.application.CyApplicationManager;
import org.cytoscape.application.events.SetCurrentNetworkListener;
import org.cytoscape.application.swing.CytoPanelComponent;
import org.cytoscape.group.CyGroupFactory;
import org.cytoscape.group.CyGroupManager;
import org.cytoscape.group.events.GroupAboutToBeRemovedListener;
import org.cytoscape.group.events.GroupAddedListener;
import org.cytoscape.group.events.GroupCollapsedListener;
import org.cytoscape.model.CyNetworkManager;
import org.cytoscape.model.events.AddedNodesListener;
import org.cytoscape.model.events.RemovedNodesListener;
import org.cytoscape.model.subnetwork.CyRootNetworkManager;
import org.cytoscape.service.util.AbstractCyActivator;
import org.cytoscape.view.model.CyNetworkView;
import org.cytoscape.view.model.CyNetworkViewManager;
import org.cytoscape.view.model.events.AboutToRemoveNodeViewsListener;
import org.cytoscape.view.vizmap.VisualMappingManager;
import org.cytoscape.work.TaskManager;
import org.osgi.framework.BundleContext;


import org.cytoscape.application.CyApplicationManager;
import org.cytoscape.event.CyEventHelper;
import org.cytoscape.group.CyGroupManager;
import org.cytoscape.group.data.CyGroupAggregationManager;
import org.cytoscape.group.events.GroupAboutToCollapseListener;
import org.cytoscape.group.events.GroupAddedListener;
import org.cytoscape.model.CyNetworkManager;
import org.cytoscape.myapp.cytoscape_app_v1.internal.data.CyGroupAggregationManagerImpl;
import org.cytoscape.myapp.cytoscape_app_v1.internal.data.CyGroupNodeSettingsTaskFactory;
import org.cytoscape.myapp.cytoscape_app_v1.internal.data.CyGroupSettingsImpl;
import org.cytoscape.myapp.cytoscape_app_v1.internal.data.CyGroupSettingsTaskFactory;
import org.cytoscape.myapp.cytoscape_app_v1.internal.data.GroupDataCollapseHandler;
import org.cytoscape.service.util.AbstractCyActivator;
import org.cytoscape.task.NodeViewTaskFactory;
import static org.cytoscape.work.ServiceProperties.*;
import org.cytoscape.work.TaskFactory;


import org.osgi.framework.BundleContext;
import java.util.Properties;


public class CyActivator extends AbstractCyActivator {

	@Override
	public void start(BundleContext context) throws Exception {
		
		
		
		CyEventHelper cyEventHelperServiceRef = getService(context,CyEventHelper.class);
		CyGroupManager cyGroupManager = getService(context,CyGroupManager.class);
		CyNetworkManager cyNetworkManager = getService(context,CyNetworkManager.class);
		CyApplicationManager cyApplicationManager = getService(context,CyApplicationManager.class);
		
		//CyApplicationManager applicationManager = getService(context, CyApplicationManager.class);
        
        // Access to all networks
        CyNetworkManager networkManager = getService(context, CyNetworkManager.class);
        
        // The CyGroupManager maintains information about all of the groups an instance of Cytoscape.
       // CyGroupManager groupManager = getService(context, CyGroupManager.class);
        
        // An interface describing a factory used for creating CyGroup objects.
        CyGroupFactory groupFactory = getService(context, CyGroupFactory.class);
        
        CyNetworkViewManager networkViewManager = getService(context, CyNetworkViewManager.class);
		
        TaskManager taskManager = getService(context, TaskManager.class);
        
		MyCytoPanel panel =  new MyCytoPanel(cyApplicationManager,networkManager,cyGroupManager,groupFactory,networkViewManager,taskManager);  
		
		// Get the generally useful handlers
				

				// Create the aggregation manager
				CyGroupAggregationManager cyAggMgr = 
					new CyGroupAggregationManagerImpl(cyGroupManager);
		    registerService(context,cyAggMgr,CyGroupAggregationManager.class, new Properties());

				// Get our Settings object
				CyGroupSettingsImpl cyGroupSettings = 
					new CyGroupSettingsImpl(cyGroupManager, cyAggMgr, cyApplicationManager);

				// Register our settings menu
		    CyGroupSettingsTaskFactory settingsFactory = 
					new CyGroupSettingsTaskFactory(cyAggMgr, 
					                               cyApplicationManager, 
					                               cyGroupSettings);

		    Properties settingsProps = new Properties();
		    settingsProps.setProperty(ID,"settingsFactory");
		    settingsProps.setProperty(PREFERRED_MENU,"Edit.Preferences");
		    settingsProps.setProperty(TITLE, "Group Preferences...");
		    settingsProps.setProperty(MENU_GRAVITY,"4.0");
		    settingsProps.setProperty(TOOL_BAR_GRAVITY,"4");
		    settingsProps.setProperty(IN_TOOL_BAR,"false");
		    registerService(context,settingsFactory,TaskFactory.class, settingsProps);

				// Now register our node-specific settings menu
		    CyGroupNodeSettingsTaskFactory nodeSettingsFactory = 
					new CyGroupNodeSettingsTaskFactory(cyGroupManager,
					                                   cyAggMgr, 
					                                   cyApplicationManager, 
					                                   cyGroupSettings);
		    settingsProps = new Properties();
		    settingsProps.setProperty(ID,"groupNodeSettingsFactory");
		    settingsProps.setProperty(PREFERRED_MENU,NODE_PREFERENCES_MENU);
		    settingsProps.setProperty(MENU_GRAVITY, "-1");
		    settingsProps.setProperty(TITLE, "Group Preferences...");
				settingsProps.setProperty(PREFERRED_ACTION, "NEW");
				settingsProps.setProperty(COMMAND, "group-node-settings");
				settingsProps.setProperty(COMMAND_NAMESPACE, "network-view");
		    registerService(context,nodeSettingsFactory,
				                NodeViewTaskFactory.class, settingsProps);

				// Make the settings available to consumers
		    registerService(context,cyGroupSettings, 
				                GroupAddedListener.class, new Properties());
		   
		    registerService(context,cyGroupSettings, 
				                CyGroupSettingsImpl.class, new Properties());

				GroupDataCollapseHandler gdcHandler =
		      new GroupDataCollapseHandler(cyGroupManager, cyGroupSettings);

		    registerService(context,gdcHandler,
				                GroupAboutToCollapseListener.class, new Properties());
	
		
		//Registro los servicios con los observadores
		registerService(context, panel, CytoPanelComponent.class, new Properties());
		registerService(context, panel, SetCurrentNetworkListener.class, new Properties());
		//registerService(context, panel, GroupCollapsedListener.class, new Properties());
        
		ViewUtil ui= new ViewUtil(cyApplicationManager, networkViewManager);
		
		registerService(context, ui, GroupCollapsedListener.class, new Properties());
		//registerService(context, ui, AddedNodesListener.class, new Properties());
		registerService(context, ui, GroupAboutToBeRemovedListener.class, new Properties());
		
		registerService(context, ui, AboutToRemoveNodeViewsListener.class, new Properties());
		//registerService(context, ui, GroupAddedListener.class, new Properties());
		
		
		
		
		
	}

}
